<template>
  <ex-dialog
    width="700px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <div class="tips">检测到您使用初始密码登录,为了您的账号安全请重新设置登录密码</div>
      <template>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="140px"
          class="demo-ruleForm"
          :disabled="disabled"
        >
          <el-row>
            <el-col :span="18">
              <el-form-item label="设置新密码：" prop="orpassword">
                <el-input
                  type="password"
                  style="width: 250px"
                  v-model="ruleForm.orpassword"
                  placeholder="请填写"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18">
              <el-form-item label="确认新密码：" prop="newPassword">
                <el-input
                  type="password"
                  style="width: 250px"
                  v-model="ruleForm.newPassword"
                  placeholder="请填写"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </div>
    <div class="button_box">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
        >保存提交</el-button
      >
    </div>
  </ex-dialog>
</template>
  <script>
import Loading from "@/components/Loading/index";
import { rsaEncrypt } from '@/utils/RSA.js'
// api
import {
  addService,
  serviceItemSelect,
  clientSelect,
} from "@/api/customer/rowDateList";
export default {
  components: {
    Loading,
  },
  props: {
    visible: Boolean,
    loginForm: Object,
  },
  data() {
    var validateNewPass = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请输入新密码'));
			} else {
				
				callback();
			}
		};
		var validateOrNewPass = (rule, value, callback) => {
      console.log("value",value)
      console.log("this.ruleForm.orpassword",this.ruleForm.orpassword)
			if (value === '') {
				callback(new Error('请再次输入新密码'));
			} else if (value != this.ruleForm.orpassword) {
				callback(new Error('两次输入密码不一致!'));
			} else {
				callback();
			}
		};
    
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "密码设置",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        orpassword: '', //新密码
        newPassword: "", //校验新密码
        password: '',
        userName: '',
      },
      rules: {
        orpassword: [
          { required: true,validator: validateNewPass,message: '请输入新密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true,validator: validateOrNewPass,trigger: 'blur' }
        ],
      },
    };
  },
  computed: {},
  created() {
    this.ruleForm.password = this.loginForm.password
    this.ruleForm.userName = this.loginForm.userName

  },
  mounted() {},
  computed: {},
  methods: {

    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.AgreeLoading = true;
  
          let jiami = rsaEncrypt(JSON.stringify(this.ruleForm)) //rsa加密
					let params = {
						encryptData: jiami
					}
          this.$store.dispatch("user/password", params)
            .then((res) => {
              this.AgreeLoading = false
              console.log(123123,res);
              this.$router.push({
                path: "/",
                query: this.otherQuery,
              });
              this.dialogVisible = true
            })
            .catch(() => {
              this.AgreeLoading = false
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  
}
.tips{
  padding: 20px 0;
  display: flex;
  width: 100%;
  justify-content: center;
  font-size: 18px;
}
.el-select,
.el-input,
.el-cascader {
  width: 400px;
}
.el-form-item__content{
  border: 1px solid #eee !important;
}
.button_box {
  display: flex;
  justify-content: center;
}

</style>
    